clear textprogressbar; clear; clc;
%% Create session, Connect devices
PROJ_PATH = ['D:\code\AutoMeasurement\example\test_ziHF2' '\'];
session=MeasurementSession.SimpleMeasurement('owner',       'ZN', ...
                                             'name',        'TestSpectrum',...
                                             'device_file', [PROJ_PATH 'device.dat'] );
                                         
session.addDescription( ...
    ['This Measurement tests the batch operation of Spectrum Module.' ...
     'Device infomation should be provide in ''device.dat''. '  ...
     'Module settings should be provide in ''settings.mat''. ' ] ...
    );

%%
zi = session.getDevice('dev1290');
plotter=zi.plotterModule(session);
spectrum = zi.spectrumModule(session);
%% 
plotter.setMonitorNode('x');
plt_data = plotter.run('0', 'total_duration', 3.0);
data=plotter.getDataNode('x');
%%
% spectrum.setParam('type', 0);
% spectrum.setParam('grid/repetitions', 100);
spectrum.setControl();
spectrum.setParam('spectrum/frequencyspan', 100e3)
spectrum.subscribeChannel('0', 'R.fft.abs.avg');
spect_data = spectrum.run();
spectrum.unsubscribeChannel();

%%
h = ziDAQ('dataAcquisitionModule');
ziDAQ('get', h, 'dataAcquisitionModule/triggernode');
ziDAQ('set', h, 'dataAcquisitionModule/triggernode', '/dev1290/demods/0/sample.R');
ziDAQ('get', h, 'dataAcquisitionModule/count');
ziDAQ('get', h, 'dataAcquisitionModule/type');
ziDAQ('get', h, 'dataAcquisitionModule/edge');
ziDAQ('get', h, 'dataAcquisitionModule/bits');
ziDAQ('get', h, 'dataAcquisitionModule/bitmask');
ziDAQ('get', h, 'dataAcquisitionModule/eventcount/mode');
ziDAQ('get', h, 'dataAcquisitionModule/delay');
ziDAQ('get', h, 'dataAcquisitionModule/duration');
ziDAQ('get', h, 'dataAcquisitionModule/level');
ziDAQ('get', h, 'dataAcquisitionModule/hysteresis');
ziDAQ('get', h, 'dataAcquisitionModule/findlevel');
ziDAQ('get', h, 'dataAcquisitionModule/bandwidth');
ziDAQ('get', h, 'dataAcquisitionModule/pulse/min');
ziDAQ('get', h, 'dataAcquisitionModule/pulse/max');
ziDAQ('get', h, 'dataAcquisitionModule/holdoff/time');
ziDAQ('get', h, 'dataAcquisitionModule/holdoff/count');
ziDAQ('get', h, 'dataAcquisitionModule/grid/mode');
ziDAQ('get', h, 'dataAcquisitionModule/grid/cols');
ziDAQ('get', h, 'dataAcquisitionModule/grid/rows');
ziDAQ('get', h, 'dataAcquisitionModule/grid/repetitions');
ziDAQ('get', h, 'dataAcquisitionModule/grid/rowrepetition');
ziDAQ('get', h, 'dataAcquisitionModule/grid/direction');
ziDAQ('get', h, 'dataAcquisitionModule/grid/waterfall');
ziDAQ('get', h, 'dataAcquisitionModule/grid/overwrite');
ziDAQ('get', h, 'dataAcquisitionModule/fft/window');
ziDAQ('get', h, 'dataAcquisitionModule/buffercount');
ziDAQ('get', h, 'dataAcquisitionModule/refreshrate');
ziDAQ('get', h, 'dataAcquisitionModule/awgcontrol');
ziDAQ('get', h, 'dataAcquisitionModule/forcetrigger');
ziDAQ('get', h, 'dataAcquisitionModule/preview');
ziDAQ('set', h, 'dataAcquisitionModule/preview', 1);
ziDAQ('get', h, 'dataAcquisitionModule/save/save');
ziDAQ('get', h, 'dataAcquisitionModule/save/directory');
ziDAQ('get', h, 'dataAcquisitionModule/triggered');
ziDAQ('set', h, 'dataAcquisitionModule/device', 'dev1290');
ziDAQ('set', h, 'dataAcquisitionModule/historylength', 100);
ziDAQ('get', h, 'dataAcquisitionModule/historylength');
ziDAQ('set', h, 'dataAcquisitionModule/bandwidth', 0.0000000);
ziDAQ('set', h, 'dataAcquisitionModule/hysteresis', 0.0100000);
ziDAQ('set', h, 'dataAcquisitionModule/level', 0.1000000);
ziDAQ('set', h, 'dataAcquisitionModule/save/directory', 'C:\Users\DELL\Documents\Zurich Instruments\LabOne\WebServer');
ziDAQ('set', h, 'dataAcquisitionModule/save/directory', 'C:\Users\DELL\Documents\Zurich Instruments\LabOne\WebServer');
ziDAQ('set', h, 'dataAcquisitionModule/clearhistory', 1);
ziDAQ('set', h, 'dataAcquisitionModule/clearhistory', 1);
ziDAQ('set', h, 'dataAcquisitionModule/bandwidth', 0.0000000);
ziDAQ('set', h, 'dataAcquisitionModule/level', 1.3200000);
ziDAQ('set', h, 'dataAcquisitionModule/hysteresis', 0.0000000);
ziDAQ('set', h, 'dataAcquisitionModule/endless', 0);
ziDAQ('subscribe', h, '/dev1290/demods/0/sample.R.avg');
ziDAQ('execute', h);
result = [];
while ~ziDAQ('finished', h)
  pause(1);
  result = ziDAQ('read', h);
  fprintf('Progress %0.0f%%\n', ziDAQ('progress', h) * 100);
%   Using intermediate reads you can plot an ongoing function.
end
ziDAQ('set', h, 'dataAcquisitionModule/forcetrigger', 1);
ziDAQ('finish', h);
ziDAQ('unsubscribe', h, '*');

%%

h = ziDAQ('scopeModule');
ziDAQ('set', h, 'scopeModule/mode', 3);
ziDAQ('setInt', '/dev1290/scopes/0/time', 9);
ziDAQ('set', h, 'scopeModule/averager/restart', 0);
ziDAQ('set', h, 'scopeModule/averager/weight', 100);
ziDAQ('set', h, 'scopeModule/fft/window', 1);
ziDAQ('subscribe', h, '/dev1290/scopes/0/wave');
ziDAQ('setInt', '/dev1290/scopes/0/enable', 1);
ziDAQ('execute', h);
pause(5);
result1 = ziDAQ('read', h);

ziDAQ('setInt', '/dev1290/scopes/0/enable', 0);
ziDAQ('finish', h);
ziDAQ('unsubscribe', h, '*');